package github.leavesczy.reactivehttpsamples.adapter

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter4.BaseQuickAdapter
import github.leavesczy.reactivehttpsamples.R
import github.leavesczy.reactivehttpsamples.base.BaseActivity
import github.leavesczy.reactivehttpsamples.core.mode.BridgeRecordItem
import github.leavesczy.reactivehttpsamples.databinding.AdapterHistoryItemBinding
import github.leavesczy.reactivehttpsamples.ui.resultdetail.ResultDetailActivity

class BridgeRecordAdapter : BaseQuickAdapter<BridgeRecordItem, BridgeRecordAdapter.VH>(),
    BaseQuickAdapter.OnItemClickListener<BridgeRecordItem> {

    class VH(val bind: AdapterHistoryItemBinding) : RecyclerView.ViewHolder(bind.root)
    init {
        setOnItemClickListener(this)
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): BridgeRecordAdapter.VH {
        val inflate = AdapterHistoryItemBinding.inflate(LayoutInflater.from(context), parent, false)
        return VH(inflate)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: BridgeRecordItem?) {
        holder.bind.tvBridgeName.text = item?.bridgeName
        holder.bind.tvBridgeCode.text = item?.bridgeCode
        holder.bind.tvBridgeType.text = item?.getBridgeTypeString()
        holder.bind.tvSampleType0.text = item?.getMeasureModelNumType0Str()
        holder.bind.tvSampleType1.text = item?.getMeasureModelNumType1Str()
        holder.bind.tvBridgeResult.text = item?.getResultStatusString()
        holder.bind.tvBridgeResult.setTextColor(ColorUtils.getColor(item?.getResultStatusColor()?:R.color.black))
        if(item?.isHasResult()==true){
            holder.bind.tvResultDetail.visibility=View.VISIBLE
            holder.bind.tvDoAnalysis.visibility=View.GONE
        }else if(item?.isCanAnalysis()==true){
            holder.bind.tvResultDetail.visibility=View.GONE
            holder.bind.tvDoAnalysis.visibility=View.VISIBLE
        }else{
            holder.bind.tvResultDetail.visibility=View.GONE
            holder.bind.tvDoAnalysis.visibility=View.GONE
        }


    }

    override fun onClick(
        adapter: BaseQuickAdapter<BridgeRecordItem, *>,
        view: View,
        position: Int
    ) {
        val item = getItem(position)
        if(!item!!.isHasResult()){
            return
        }
        val  bundle=Bundle()
        bundle.putSerializable("item",item)
        ActivityUtils.startActivity(bundle,ResultDetailActivity::class.java)
    }


}


